/**
 * @Author wywinstonwy
 * @Date 2022/1/9 9:12 下午
 * @Description:
 */
import 'package:demo202112/utils/common_appbar.dart';
import "package:flutter/material.dart";

class MyWillPopScope extends StatefulWidget {
  const MyWillPopScope({Key? key}) : super(key: key);

  @override
  _MyWillPopScopeState createState() => _MyWillPopScopeState();
}

class _MyWillPopScopeState extends State<MyWillPopScope> {
  DateTime? _lastPressedAt; //上次点击时间
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('WillPopScope'),
      body: _buildWillPopScope(),
    );
  }

  _buildWillPopScope() {
    return WillPopScope(
        child: Container(
          alignment: Alignment.center,
          child: const Text("1秒内连续按两次返回键退出"),
        ),
        onWillPop: () async {
          if (_lastPressedAt != null ||
              DateTime.now().difference(_lastPressedAt!) >
                  const Duration(seconds: 1)) {
            //两次点击间隔超过1秒则重新计时
            _lastPressedAt = DateTime.now();
            return false;
          }

          return true;
        });
  }


}
